
import os
import pygame,sys
from pygame.locals import *
pygame.init()
game_clock = pygame.time.Clock()
game_speed = 60
game_screen_width,game_screen_height = 640,480
pygame.display.set_caption("pysnake")
game_screen=pygame.display.set_mode((game_screen_width,game_screen_height)) 
game_field = game_screen.get_rect()
game_running = True
game_bgcolor = 0,0,0
game_linecolor = 0,0,0
square_color = 255,255,255
square_color2 = 255,255,255
food_x,food_y = 0,300
CELL_SIZE = 20 
square_rect = pygame.Rect(0,0,CELL_SIZE,CELL_SIZE)
UP,DOWN,LEFT,RIGHT = ((0,-CELL_SIZE),(0,CELL_SIZE),
                      (-CELL_SIZE,0),(CELL_SIZE,0))
square_direction = RIGHT 
square_turn = RIGHT
square_speed = 5 
square_delay = 1000 / square_speed 
square_time2move = pygame.time.get_ticks() + square_delay
while game_running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
        elif event.type == pygame.KEYDOWN:
            if square_direction in [LEFT,RIGHT]:                
                if event.key == pygame.K_UP:
                    square_turn = UP
                elif event.key == pygame.K_DOWN:
                    square_turn = DOWN
            elif square_direction in [UP,DOWN]:
                if event.key == pygame.K_LEFT:
                    square_turn = LEFT
                elif event.key == pygame.K_RIGHT:
                    square_turn = RIGHT
    if pygame.time.get_ticks() >= square_time2move:
        square_time2move = pygame.time.get_ticks() + square_delay
        square_direction = square_turn

        square_rect = square_rect.move(square_direction)
    game_screen.fill(game_bgcolor)
    for i in range(CELL_SIZE,game_screen_width,CELL_SIZE):
        pygame.draw.line(game_screen,game_linecolor,
                         (i,0),(i,game_screen_height))
    for i in range(CELL_SIZE,game_screen_height,CELL_SIZE):
        pygame.draw.line(game_screen,game_linecolor,
                         (0,i),(game_screen_width,i))

    game_screen.fill(square_color,square_rect)
    game_screen.fill(square_color2,square_rect.inflate(-4,-4))
    pygame.draw.rect(game_screen,square_color,(food_x,food_y,
                                               CELL_SIZE,CELL_SIZE))
    pygame.display.flip()
    game_clock.tick(game_speed)
pygame.quit()
sys.exit(0)
